package com.itguigu.glimail.seckill.controller;
import com.itguigu.common.utils.R;
import com.itguigu.glimail.seckill.service.SeckillService;
import com.itguigu.glimail.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SeckillController {
    @Autowired
    SeckillService seckillService;
    /**
     * 返回当前时间可以参与的秒杀商品信息
     */
    @ResponseBody
    @GetMapping("/currentSeckillSkus")
    public R getCurrentSeckillSkus() {
        List<SeckillSkuRedisTo> list = seckillService.getCurrentSeckillSkus();
        return R.ok().setData(list);
    }
    @ResponseBody
    @GetMapping("/sku/seckill/{skuId}")
    public R skuSeckillInfo(@PathVariable("skuId") Long skuId) {
       SeckillSkuRedisTo list = seckillService.getSkuSeckillInfo(skuId);
        return R.ok().setData(list);
    }

    @GetMapping("/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) {
        System.out.println(num);
        String orderSn = seckillService.seckill(killId,key,num);
        model.addAttribute("orderSn", orderSn);
        return "success";
    }


}
